package com.ktjy.ktmall.service.impl;

import com.ktjy.ktmall.dao.TMenuDao;
import com.ktjy.ktmall.dao.impl.TMenuDaoImpl;
import com.ktjy.ktmall.entity.TMenu;
import com.ktjy.ktmall.mapper.TMenuMapper;
import com.ktjy.ktmall.service.TMenuService;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class TMenuServiceImpl implements TMenuService {
    TMenuDao tMenuDao = new TMenuDaoImpl();

    @Override
    public List<TMenu> getMenuList() throws IOException {
//
//        List<TMenu>  list= tMenuDao.getMenus();
//
//
//        for (int i = 0; i <list.size() ; i++) {
//            TMenu tempMenu=list.get(i);
//            List<TMenu> childrens=tempMenu.getChildren();
//            if(childrens==null){
//                childrens=new ArrayList<>();
//            }
//            for (int x = 0; x <list.size() ; x++) {
//                    TMenu children=list.get(x);
//                    if(children.getPsPid()==tempMenu.getPsId()){
//                        childrens.add(children);
//                    }
//            }
//            tempMenu.setChildren(childrens);
//        }
//
//
//        List<TMenu > resultList = new ArrayList<>();
//        for (int i = 0; i <list.size() ; i++) {
//            if(list.get(i).getPsPid()==0){
//                resultList.add(list.get(i));
//            }
//        }
//        return resultList;



        //1、读取mybatis核心配置文件
        String config="mybatis-config.xml";
        InputStream is = Resources.getResourceAsStream(config);

        //2、通过核心配置文件创建 sqlsessionFactory
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);

        SqlSession  session=sqlSessionFactory.openSession();
//        //3、获取mapper接口实现类 调用需要执行的方法
//        List<TMenu> result=session.getMapper(TMenuMapper.class).getMenuByMenuName(1,"订单中心");

        Map tMenu = new HashMap();
        tMenu.put("psName","中");

        //3、获取mapper接口实现类 调用需要执行的方法
        List<TMenu> result=session.getMapper(TMenuMapper.class).getMenuByMap(tMenu);
        session.close();

        return result;
    }


    public List<TMenu> getMenuList2() {
        //查所有的一级
        List<TMenu>  firstList= tMenuDao.getMenusByIds(0L);
        for (int i = 0; i < firstList.size(); i++) {
            //查二级
            List<TMenu> secondList=tMenuDao.getMenusByIds(firstList.get(i).getPsId());
            //查三级
            for (int j = 0; j <secondList.size() ; j++) {
                secondList.get(j).setChildren(tMenuDao.getMenusByIds(secondList.get(j).getPsId()));
            }
            firstList.get(i).setChildren(secondList);
        }
        return firstList;
    }

    @Override
    public Integer getMenuCount() throws IOException {

        //1、读取mybatis核心配置文件
        String config="mybatis-config.xml";
        InputStream is = Resources.getResourceAsStream(config);

        //2、通过核心配置文件创建 sqlsessionFactory
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);

        SqlSession  session=sqlSessionFactory.openSession();
        //3、获取mapper接口实现类 调用需要执行的方法
        int result=session.getMapper(TMenuMapper.class).getMenuCount();

        session.close();

        return result;
    }

    @Override
    public Integer addMenu(TMenu menu) throws IOException {

        //1、读取mybatis核心配置文件
        String config="mybatis-config.xml";
        InputStream is = Resources.getResourceAsStream(config);

        //2、通过核心配置文件创建 sqlsessionFactory
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);

        SqlSession  session=sqlSessionFactory.openSession();

        //3、获取mapper接口实现类 调用需要执行的方法
        int result=session.getMapper(TMenuMapper.class).addMenu(menu);

        session.commit();

        session.close();

        return result;
    }

    @Override
    public Integer deleteMenu(Integer psId) throws IOException {
        //1、读取mybatis核心配置文件
        String config="mybatis-config.xml";
        InputStream is = Resources.getResourceAsStream(config);

        //2、通过核心配置文件创建 sqlsessionFactory
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);

        SqlSession  session=sqlSessionFactory.openSession(true);

        //3、获取mapper接口实现类 调用需要执行的方法
        int result=session.getMapper(TMenuMapper.class).deleteMenu(psId);

        session.close();

        return result;
    }

    @Override
    public Integer updateMenu(TMenu menu) throws IOException {
        //1、读取mybatis核心配置文件
        String config="mybatis-config.xml";
        InputStream is = Resources.getResourceAsStream(config);

        //2、通过核心配置文件创建 sqlsessionFactory
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);

        SqlSession  session=sqlSessionFactory.openSession(true);

        //3、获取mapper接口实现类 调用需要执行的方法
        int result=session.getMapper(TMenuMapper.class).updateMenu(menu);

        session.close();

        return result;
    }
}
